#include "ChessApp.h"

ChessApp::ChessApp(HINSTANCE hInstance)
	:D3DApp(hInstance)
	,m_EyePos(D3DXVECTOR3(0.0f,2.0f,-2.0f))
{
	D3DXMatrixIdentity(&m_matView);
}


ChessApp::~ChessApp(void)
{
}

void ChessApp::initApp()
{
	D3DApp::initApp();

//////////////////Resource Manager////////////////////////////////////////////////////////
	ResourcesManager::Init(md3dDevice);

/////////////////Scene Manager/////////////////////////////////////////////////////////
	m_SceneMgr.Init(md3dDevice);

//////////////////Message System//////////////////////////////////////////
	MessageQueryer::Instance()->Init();

	m_Chess.Init(md3dDevice,mhAppInst);

	//show the main windows
	SetForegroundWindow(mhMainWnd);
	SetFocus(mhMainWnd);
	ShowWindow(mhMainWnd, SW_SHOW);

	UpdateWindow(mhMainWnd);
	BringWindowToTop(mhMainWnd);
}

void ChessApp::onResize()
{
	D3DApp::onResize();

	float aspect = (float)mClientWidth/mClientHeight;
	D3DXMatrixPerspectiveFovLH(&m_matProj, 0.25f*PI, aspect, 0.5f, 1000.0f);
	GlobalMessage::Instance()->SetProjMatrix(m_matProj);
}

void ChessApp::updateScene( float dt )
{
	D3DApp::updateScene(dt);

	//set the global message time from last frame
	GlobalMessage::Instance()->SetTimeFromLastFrame(dt);

	//dispatch message
	MessageDispatcher::Instance()->DispatchKeyboardDownMessage();

	m_Chess.UpdateScene(dt);
}

void ChessApp::drawScene()
{
	D3DApp::drawScene();

	md3dDevice->OMSetDepthStencilState(0,0);
	float blendFactor[] = {0.0f, 0.0f, 0.0f, 0.0f};
	md3dDevice->OMSetBlendState(0, blendFactor, 0xffffffff);

///////////////Rendering Scene///////////////////////////////////////////////////////////
//	m_SceneMgr.RenderOneFrame();
	m_Chess.Draw();


//////////////////////////////////////////////////////////////////////////
	RECT R = {5, 5, 0, 0};
	md3dDevice->RSSetState(0);
	mFont->DrawText(0, mFrameStats.c_str(), -1, &R, DT_NOCLIP, BLACK);

	mSwapChain->Present(0, 0);
}

LRESULT ChessApp::msgProc( UINT msg, WPARAM wParam, LPARAM lParam )
{
	switch( msg )
	{
////////////////////////Mouse Message////////////////////////////////
	case WM_MOUSEMOVE:
		MessageDispatcher::Instance()->DispatchMouseMessage(MMsg_Move,lParam);
		return 0;

	case WM_LBUTTONDOWN:
		MessageDispatcher::Instance()->DispatchMouseMessage(MMsg_LeftButtonDown,lParam);
		return 0;

	case WM_LBUTTONUP:
		MessageDispatcher::Instance()->DispatchMouseMessage(MMsg_LeftButtonUp,lParam);
		return 0;

	case WM_RBUTTONDOWN:
		MessageDispatcher::Instance()->DispatchMouseMessage(MMsg_RightButtonDown,lParam);
		return 0;

	case WM_RBUTTONUP:
		MessageDispatcher::Instance()->DispatchMouseMessage(MMsg_RightButtonUp,lParam);
		return 0;

	case WM_MOUSEWHEEL:
		if((short) HIWORD (wParam) > 0)
		{
			MessageDispatcher::Instance()->DispatchMouseMessage(MMsg_WheelForward,lParam);
		}
		else
		{
			MessageDispatcher::Instance()->DispatchMouseMessage(MMsg_WheelBackward,lParam);
		}
		return 0;
////////////////////////Keyboard Message//////////////////////////////////
	case WM_CHAR:
		MessageDispatcher::Instance()->DispatchKeyboardMessage(wParam);
		return 0;
//////////////////////////////////////////////////////////////////////////
	}

	return D3DApp::msgProc(msg, wParam, lParam);
}

void ChessApp::calculateAI()
{
	m_Chess.CalculateAI();
}
